import Vue from 'vue'
import VueRouter from 'vue-router'
import routeList from './routes'
const routes = [...routeList]
Vue.use(VueRouter)

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next()是一个函数 表示放行
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.localStorage.getItem('Authorization')
  // 如果token不存在表示没有登录 则跳转到登录页面
  if (!tokenStr) return next('/login')
  next()
})

export default router
